<?php
	require_once 'class.phieuthu.php';
	
	class giangvien{
		public function add_giangvien($f_car,$f_hovaten,$f_gioitinh,$f_tuoi,$f_diachi,$f_chuyenloaihinh, $f_phanmemsudung,$sdt,$mail)
		{	
			// added & editted by thinhnc 2013-04-07
			$sql = "INSERT INTO tbl_giangvien(f_hovaten,f_gioitinh,f_tuoi,f_diachi,f_chuyenloaihinh,f_phanmemsudung,sdt,mail) VALUES ('$f_hovaten','$f_gioitinh','$f_tuoi','$f_diachi','$f_chuyenloaihinh','$f_phanmemsudung','$sdt','$mail')";
			$a=mysql_query($sql);
			if($a) {
				$gvid = mysql_insert_id();
				foreach ($f_car as $carid)
				{
					$sql2 = "INSERT INTO tbl_lichranhgv(gvid,carid) values($gvid,$carid)";
					$b=mysql_query($sql2) or die(mysql_error());					
				}
				echo 'Thanh cong';
			}
			else{
				echo 'Khong thanh cong';
			}
			// end thinhnc			
		}		
		
		public function edit_giangvien($f_id,$f_oldcar,$f_car,$f_hovaten,$f_gioitinh,$f_tuoi,$f_diachi,$f_chuyenloaihinh, $f_phanmemsudung,$sdt,$mail)
		{	
			// added thinhnc 2013-04-07
			
			// if new lichranh type is used, remove the old one.
			if (is_array($f_car))
				$f_oldcar = "";
			
			$sql = "UPDATE tbl_giangvien
					SET 	f_hovaten = '$f_hovaten',
							f_gioitinh = '$f_gioitinh',
							f_tuoi = '$f_tuoi',
							f_diachi = '$f_diachi',
							f_chuyenloaihinh = '$f_chuyenloaihinh',
							f_phanmemsudung = '$f_phanmemsudung',
							f_car = '$f_oldcar',
							sdt = '$sdt',
							mail = '$mail'
					WHERE id = '$f_id'";
			$delAllCar = "DELETE FROM tbl_lichranhgv WHERE gvid = '$f_id'";
			mysql_query($delAllCar) or die(mysql_error());
					
			$a=mysql_query($sql) or die(mysql_error());
			if($a) {
				if ($f_oldcar == "")
					foreach ($f_car as $carid)
					{
							$sql2 = "INSERT INTO tbl_lichranhgv(gvid,carid) values($f_id,$carid)";
							$b=mysql_query($sql2) or die(mysql_error());										
					}
				echo 'Thanh cong';
			}
			else{
				echo 'Khong thanh cong';
			}
			// end thinhnc			
		}

		// thinhnc 2013-04-16
		/*
		function getLichRanhGV()
			Get lich ranh cua giao vien
		*/
		public function getLichRanhGV($gvid) {
			if (is_nan($gvid)) {
				return null;
			}
						
			$query = mysql_query("SELECT * FROM tbl_lichranhgv
								WHERE tbl_lichranhgv.gvid = '".$gvid."'") or die(mysql_error());
			$tkb = array();
			while ($rows = mysql_fetch_array($query))
			{
				$tkb[] = $rows['carid'];
			}
			return $tkb;
		}
		/*
		function getCARID()
			Get id ca ranh cua giao vien
		*/
		public function getCaRID($ngay,$buoi) {
			$ngay = explode("-", str_replace("/","-",$ngay));
			$date = gmmktime(0, 0, 0, (int)$ngay[1], (int)$ngay[0], (int)$ngay[2]);
			$thu = gmdate("N", $date);
			$caRID = $thu + ($buoi-1)*7;
			return $caRID;
		}
		// thinhnc 2013-04-15
		/*
		function setLichRanhGV()
			Cap nhat lai lich ranh cua giao vien khi co su thay doi trong viec sap lop cho hoc vien
			=> Chi tac dung len nhung ngay moi duoc chon.
		*/
		public function setLichRanhGV($gvid, $f_tkb, $f_sobuoi, $f_ngaybatdau, $f_hstart, $f_hend) {
			if ($gvid == '' || 
				$f_tkb == '' ||
				$f_sobuoi == '' ||
				$f_ngaybatdau == '' ||
				$f_hstart == '' ||
				$f_hend == '') {
				return false;
			}
			
			$phieuthu = new phieuthu();
			// Tinh ngay ket thuc khoa hoc de dua vao field available_on cua lichphanconggv
			$ngaybatdau = str_replace("/","-",$f_ngaybatdau);
			$ngayketthuc = $phieuthu->tinhNgayKetThuc($f_ngaybatdau, $f_sobuoi, $f_tkb, $f_hstart, $f_hend);	

			// unset old values
			$this->unsetLichRanhGV($gvid, $f_tkb, $ngaybatdau, $ngayketthuc, $f_hstart, $f_hend);					
			
			$query = mysql_query("SELECT * FROM tbl_lichranhgv LEFT JOIN tbl_giangvien ON  tbl_lichranhgv.gvid = tbl_giangvien.id
								WHERE tbl_giangvien.id = '".$gvid."'") or die(mysql_error()); // tim gv
			
			// tim tkb bat buoc ranh
			$tkb = $this->getTKB($f_tkb, $f_hstart, $f_hend);
		
			while ($rows = mysql_fetch_array($query))
			{
				if(in_array($rows['carid'], $tkb))
				{					
					$upd_query = "INSERT INTO tbl_lichphanconggv(gvid,carid,taken_on,available_on) VALUES ('".$gvid."','".$rows['carid']."','".$ngaybatdau."','".$ngayketthuc."')";
					$sql = mysql_query($upd_query) or die(mysql_error());
					if(!$sql) return false;
				}
			}
			// + reputation point
			$rep_query = "UPDATE tbl_giangvien SET f_tinnhiem = f_tinnhiem + 1 WHERE id = '".$gvid."'";
			$rep_sql = mysql_query($rep_query) or die(mysql_error());
			if ($rep_sql)
				return true;
			return false;
		}
		
		/*
		function unsetLichRanhGV()
			Cap nhat lai lich ranh cua giao vien khi co su thay doi trong viec sap lop cho hoc vien
		*/		
		public function unsetLichRanhGV($gvid, $f_tkb, $f_ngaybatdau, $f_ngayketthuc, $f_hstart, $f_hend) {
			if ($gvid == '' || 
				$f_tkb == '' ||				
				$f_ngaybatdau == '' ||
				$f_ngayketthuc == '' ||
				$f_hstart == '' ||
				$f_hend == '') {
				return false;
			}
			$query = mysql_query("SELECT * FROM tbl_lichranhgv LEFT JOIN tbl_giangvien ON  tbl_lichranhgv.gvid = tbl_giangvien.id
								WHERE tbl_giangvien.id = '".$gvid."'"); // tim gv
			
			$ngaybatdau = str_replace("/","-",$f_ngaybatdau);
			$ngayketthuc = str_replace("/","-",$f_ngayketthuc);
			
			// tim tkb bat buoc ranh
			$tkb = $this->getTKB($f_tkb, $f_hstart, $f_hend);
	
			while ($rows = mysql_fetch_array($query))
			{
				if(in_array($rows['carid'], $tkb))
				{
					$upd_query = "DELETE FROM tbl_lichphanconggv 
									WHERE gvid = '".$gvid."'
									AND carid = '".$rows['carid']."' 
									AND taken_on = '".$ngaybatdau."'
									AND available_on = '".$ngayketthuc."'";
					$sql = mysql_query($upd_query);
					if(!$sql) return false;
				}
			}
			// + reputation point
			$rep_query = "UPDATE tbl_giangvien SET f_tinnhiem = f_tinnhiem - 1 WHERE id = '".$gvid."' AND f_tinnhiem > 0";
			$rep_sql = mysql_query($rep_query) or die(mysql_error());
			if ($rep_sql)
				return true;
			return false;
		}
		
		/*
		function getTKB()
			Tu thoi khoa bieu $f_tkb (hai-tu-sau, ba-nam-bay,...), gio bat dau $_hstart, gio ket thuc $_hend,
			tinh ra cac ca hoc (sang/chieu/toi) trong cac ngay (t2-cn) ma gv bat buoc phai AVAILABLE.
			=> dua vao ket qua tra ve, loc ra cac gv co thoi khoa bieu RANH thoa voi ket qua nay.
		*/
		public function getTKB($f_tkb, $f_hstart, $f_hend) {
			if ($f_hstart >= 8 && $f_hend <= 12)
				$ca = 1;
			elseif ($f_hstart >= 13 && $f_hend <= 17)
				$ca = 2;
			elseif ($f_hstart >= 18 && $f_hend <= 22)
				$ca = 3;
			else
				$ca = 0;
			
			$caSang = array(1,2,3,4,5,6,7);
			$caChieu = array(8,9,10,11,12,13,14);
			$caToi = array(15,16,17,18,19,20,21);
			
			$cahoc = array();
			switch((int)$ca)	
			{
				case 0: // Khac
					if ($f_hstart >= 8 && 13 <= $f_hend && ($f_hend <= 17)) // Sang + Chieu
						$cahoc = array_merge($caSang, $caChieu);
					elseif ($f_hstart >= 8 && (18 <= $f_hend) && ($f_hend <= 22)) // Sang + Chieu + Toi
						$cahoc = array_merge($caSang, $caChieu, $caToi);
					elseif ($f_hstart >= 13 && (18<= $f_hend) && ($f_hend <= 22)) // Chieu + Toi
						$cahoc = array_merge($caChieu, $caToi);
					break;
				case 1: // Sang
					$cahoc = $caSang;
					break;
				case 2: // Chieu
					$cahoc = $caChieu;
					break;
				case 3: // Toi
					$cahoc = $caToi;
					break;
				default:	
			}
			$tkb = array();
			switch ((int)$f_tkb)
			{
				case 0: // Ca tuan
					$tkb = array_intersect(array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21), $cahoc);
					break;
				case 1: // 2-4-6
					$tkb = array_intersect(array(1,3,5,8,10,12,15,17,19), $cahoc);
					break;
				case 2: // 3-5-7
					$tkb = array_intersect(array(2,4,6,9,11,13,16,18,20), $cahoc);
					break;
				case 3: // t7-cn
					$tkb = array_intersect(array(6,7,13,14,20,21), $cahoc);
					break;
				case 4: // cn
					$tkb = array_intersect(array(7,14,21), $cahoc);
					break;
				default:	
			}
			return $tkb;
		}
	}
?>